// valuation.js
const app = getApp()
import {requestApi} from '../../../api'
import { formatImage } from '../../../utils/formatImage'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    channel: '',
    hidden: true,
    tradeNo: '',          // 订单号
    tradeDetail: '',      // 订单信息
    uploadImage: [],      // 上传的图片信息
    goodStar: 5,         // 商品评价
    goodText: '非常好',
    deliveryStar: 5,     // 发货速度
    deliveText: '非常好',
    serviceStar: 5,      // 服务态度
    serviceText: '非常好',
    descStar: 5,         // 描述相符
    descText: '非常好',
    timeDifference: 0, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channel = options.channel || 'wx_xcx'
    let tradeNo = options.trade_no

    this.setData({
      tradeNo: tradeNo,
      channel: channel,
    });

    this.getTradeDetailData(tradeNo, channel)
  },

  // 获取订单信息
  getTradeDetailData: function (tradeNo, channel) {
    let tradeDetail

    app.request({
      url: requestApi.orderDetailUrl,
      data: {
        trade_no: tradeNo,
        channel: channel,
      }
    }, (data) => {
      if (data.original.trade_detail.product_cover_image && data.original.trade_detail.product_cover_image.path) {
        data.original.trade_detail.product_cover_image.new_path = formatImage.cdnImage(data.original.trade_detail.product_cover_image.path, formatImage.THUMBNAIL_S)
      }
      
      tradeDetail = data.original.trade_detail

      data.original.updatetime = data.original.updatetime.replace(/-/g, '/')

      let time = Math.floor(new Date(data.original.updatetime) / 1000) - Math.floor(new Date() / 1000)

      this.setData({
        tradeDetail: tradeDetail,
        timeDifference: time
      })
    }, (error) => {
      var error = error || '响应数据失败'
      wx.showModal({
        title: '提示',
        content: error,
        showCancel: false
      })
    })
  },

  // 上传图片
  uploadImage: function () {
    let that = this

    wx.chooseImage({
      count: 1,
      success: res => {
        // filePath = res.tempFilePaths[0]

        this.setData({
          hidden: false
        })

        that.uploadFile(res.tempFiles)
      },
    })
  },

  // 图片上传验证
  uploadFile: function (tempFiles) {
    const tempFilePaths = tempFiles[0].path
    const tempFileSize = tempFiles[0].size
    const tempArr = tempFilePaths.split('.')
    const tempFileType = tempArr[tempArr.length - 1]
    const date = new Date()
    const timestamp = Math.floor(new Date() / 1000) + this.data.timeDifference // 时间戳 秒
    const time = `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}`
    let that = this

    let uploadImage = this.data.uploadImage
    let imageInfo = {
        path: '',
        width: '',
        height: ''
      }

    let authPath = 'mars' + `/${time}`

    const params = {
      auth_path: authPath,
      file_size: tempFileSize,
      timestamp: timestamp,
      file_type: tempFileType,
    }

    app.request({
      url: requestApi.webOssAuthUrl,
      methods: 'GET',
      data: params
    }, (res) => {
      let ossKey = res.original.oss_key

      wx.uploadFile({
        url: ossKey.host,
        filePath: tempFilePaths,
        name: 'file',
        formData: {
          name: tempFilePaths,
          key: ossKey.auth_path,
          OSSAccessKeyId: ossKey.accessid,
          policy: ossKey.policy,
          signature: ossKey.signature,
          success_action_status: '200',
        },
        success() {
          that.setData({
            hidden: true
          })

          let path = ossKey.auth_path,
            pathFull = formatImage.cdnImage(path, formatImage.THUMBNAIL_M)
            
          imageInfo.path = path
          imageInfo.path_cn = pathFull

          wx.getImageInfo({
            src: tempFilePaths,
            success(res) {
              console.log(res)
              imageInfo.width = res.width
              imageInfo.height = res.height

              uploadImage.push(imageInfo)
              that.setData({
                uploadImage: uploadImage
              })
            }
          })
        },
        fail(error) {
          wx.showModal({
            title: '提示',
            content: error,
            showCancel: false
          })
        }
      })

    }, (error) => {
      wx.showModal({
        title: '提示',
        content: error,
        showCancel: false
      })
      this.setData({
        hidden: true
      })
    })
  },

  // 商品评分
  goodsEvaluate: function (e) {
    let goodStar = e.currentTarget.id
    let goodText = ''

    switch (goodStar) {
      case '1':
        goodText = '非常差'
        break;
      case '2':
        goodText = '差'
        break;
      case '3':
        goodText = '一般'
        break;
      case '4':
        goodText = '好'
        break;
      case '5':
        goodText = '非常好'
        break;
      default:
        break;
    }

    this.setData({
      goodStar: goodStar,
      goodText: goodText
    })
  },

  goodsDelivery: function (e) {
    let deliveryStar = e.currentTarget.id
    let deliveText = ''

    switch (deliveryStar) {
      case '1':
        deliveText = '非常差'
        break;
      case '2':
        deliveText = '差'
        break;
      case '3':
        deliveText = '一般'
        break;
      case '4':
        deliveText = '好'
        break;
      case '5':
        deliveText = '非常好'
        break;
      default:
        break;
    }

    this.setData({
      deliveryStar: deliveryStar,
      deliveText: deliveText
    })
  },

  goodsService: function (e) {
    let serviceStar = e.currentTarget.id
    let serviceText = ''

    switch (serviceStar) {
      case '1':
        serviceText = '非常差'
        break;
      case '2':
        serviceText = '差'
        break;
      case '3':
        serviceText = '一般'
        break;
      case '4':
        serviceText = '好'
        break;
      case '5':
        serviceText = '非常好'
        break;
      default:
        break;
    }

    this.setData({
      serviceStar: serviceStar,
      serviceText: serviceText
    })
  },

  goodsDesc: function (e) {
    let descStar = e.currentTarget.id
    let descText = ''

    switch (descStar) {
      case '1':
        descText = '非常差'
        break;
      case '2':
        descText = '差'
        break;
      case '3':
        descText = '一般'
        break;
      case '4':
        descText = '好'
        break;
      case '5':
        descText = '非常好'
        break;
      default:
        break;
    }

    this.setData({
      descStar: descStar,
      descText: descText
    })
  },

  // 确认评价
  submitEvaluate: function (e) {
    let commentContent = e.detail.value.comment,
      data = this.data,
      params = {
        'trade_no': data.tradeDetail.trade_no,
        'buyer_uid': data.tradeDetail.buyer_uid,
        'seller_uid': data.tradeDetail.seller_uid,
        'gid': data.tradeDetail.gid,
        'comment_star': data.goodStar,
        'delivery_star': data.deliveryStar,
        'service_star': data.serviceStar,
        'desc_star': data.descStar,
        'comment_image_list': data.uploadImage,
        'comment_content': commentContent,
      }

    // 调用网络请求
    app.request({
      // url: API_URL_TRADE_COMMENT,
      url: requestApi.tradeCommentUrl,
      method: 'POST',
      data: params
    }, (data) => {
      wx.showToast({
        title: '评价成功',
      })
      setTimeout(function(){
        wx.redirectTo({
          url: '../tradeList/tradeList?tradeStatus=5',
        })
      }, 1000) 
    }, (error) => {
      wx.showModal({
        title: '提示',
        content: error,
        showCancel: false
      })
    })
  },
})